{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "Kqa3GIVdqT9N",
      "metadata": {
        "id": "Kqa3GIVdqT9N"
      },
      "source": [
        "赛题链接: [2021阿里云供应链大赛](https://tianchi.aliyun.com/competition/entrance/531934/introduction)\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "h0OZTQZ3jJJ3",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "h0OZTQZ3jJJ3",
        "outputId": "ddb741c9-a12e-468c-e3e5-6843e73a6e3d"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "Cloning into 'AutoX'...\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "^C\n"
          ]
        }
      ],
      "source": [
        "# 安装autox\n",
        "\n",
        "!pip install pytorch_tabnet\n",
        "!pip install ./AutoX"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5_z2e5bkjYXJ",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5_z2e5bkjYXJ",
        "outputId": "ce1640a3-395e-42d2-8032-1443942209e5"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Archive:  tianchi_clp.zip\n",
            "   creating: tianchi_clp/\n",
            "  inflating: tianchi_clp/demand_train_A.csv  \n",
            "  inflating: __MACOSX/tianchi_clp/._demand_train_A.csv  \n",
            "  inflating: tianchi_clp/geo_topo.csv  \n",
            "  inflating: __MACOSX/tianchi_clp/._geo_topo.csv  \n",
            "  inflating: tianchi_clp/product_topo.csv  \n",
            "  inflating: __MACOSX/tianchi_clp/._product_topo.csv  \n",
            "  inflating: tianchi_clp/inventory_info_A.csv  \n",
            "  inflating: __MACOSX/tianchi_clp/._inventory_info_A.csv  \n",
            "  inflating: tianchi_clp/weight_A.csv  \n",
            "  inflating: __MACOSX/tianchi_clp/._weight_A.csv  \n",
            "  inflating: tianchi_clp/demand_test_A.csv  \n",
            "  inflating: __MACOSX/tianchi_clp/._demand_test_A.csv  \n"
          ]
        }
      ],
      "source": [
        "# tianchi_clp.zip中包含 赛事链接中下载的所有csv文件\n",
        "!unzip tianchi_clp.zip"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "2R0k2AnSm24u",
      "metadata": {
        "id": "2R0k2AnSm24u"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import pandas as pd"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "18e18021",
      "metadata": {
        "id": "18e18021"
      },
      "source": [
        "## 数据预处理"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "9f2083d9",
      "metadata": {
        "id": "9f2083d9"
      },
      "outputs": [],
      "source": [
        "\n",
        "path = f'./data'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "4e24732e",
      "metadata": {
        "id": "4e24732e"
      },
      "outputs": [],
      "source": [
        "# 赛题数据demand_test_A中给了标签，我们需要将它删掉。同时我们顺便删掉无用的'Unnamed: 0'列\n",
        "\n",
        "demand_train_A = pd.read_csv(f'{path}/demand_train_A.csv')\n",
        "demand_test_A = pd.read_csv(f'{path}/demand_test_A.csv')\n",
        "\n",
        "demand_train_A.drop('Unnamed: 0', axis=1, inplace=True)\n",
        "demand_test_A.drop(['Unnamed: 0', 'qty'], axis=1, inplace=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "id": "4d43adc9",
      "metadata": {
        "id": "4d43adc9",
        "scrolled": true
      },
      "outputs": [],
      "source": [
        "# 将 demand_train_A, demand_test_A 保存为train.csv, test.csv\n",
        "demand_train_A.to_csv(path + '/train.csv', index = False)\n",
        "demand_test_A.to_csv(path + '/test.csv', index = False)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4fea2309",
      "metadata": {
        "id": "4fea2309"
      },
      "source": [
        "## 导入所需的包"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "9185f791",
      "metadata": {
        "id": "9185f791",
        "scrolled": false
      },
      "outputs": [],
      "source": [
        "from autox import AutoX"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "91ebd124",
      "metadata": {
        "id": "91ebd124"
      },
      "source": [
        "## 初始化AutoX类"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "69ac2548",
      "metadata": {
        "id": "69ac2548"
      },
      "outputs": [],
      "source": [
        "# 数据集是多表数据集，需要配置表关系\n",
        "relations = [\n",
        "    {\n",
        "            \"related_to_main_table\": \"true\", # 是否为和主表的关系\n",
        "            \"left_entity\": \"train.csv\",  # 左表名字\n",
        "            \"left_on\": [\"product\"],  # 左表拼表键\n",
        "            \"right_entity\": \"product_topo.csv\",  # 右表名字\n",
        "            \"right_on\": [\"product_level_2\"], # 右表拼表键\n",
        "            \"type\": \"1-1\" # 左表与右表的连接关系\n",
        "        },  # train.csv和product_topo.csv两张表是1对1的关系，拼接键为train.csv中的product列 和 product_topo.csv中的product_level_2列\n",
        "    {\n",
        "            \"related_to_main_table\": \"true\", # 是否为和主表的关系\n",
        "            \"left_entity\": \"test.csv\",  # 左表名字\n",
        "            \"left_on\": [\"product\"],  # 左表拼表键\n",
        "            \"right_entity\": \"product_topo.csv\",  # 右表名字\n",
        "            \"right_on\": [\"product_level_2\"], # 右表拼表键\n",
        "            \"type\": \"1-1\" # 左表与右表的连接关系\n",
        "        },  # test.csv和product_topo.csv两张表是1对1的关系，拼接键为test.csv中的product列 和 product_topo.csv中的product_level_2列\n",
        "    {\n",
        "            \"related_to_main_table\": \"true\", # 是否为和主表的关系\n",
        "            \"left_entity\": \"train.csv\",  # 左表名字\n",
        "            \"left_on\": [\"geography\"],  # 左表拼表键\n",
        "            \"right_entity\": \"geo_topo.csv\",  # 右表名字\n",
        "            \"right_on\": [\"geography_level_3\"], # 右表拼表键\n",
        "            \"type\": \"1-1\" # 左表与右表的连接关系\n",
        "        },  # train.csv和geo_topo.csv两张表是1对1的关系，拼接键为train.csv中的geography列 和 geo_topo.csv中的geography_level_3列\n",
        "    {\n",
        "            \"related_to_main_table\": \"true\", # 是否为和主表的关系\n",
        "            \"left_entity\": \"test.csv\",  # 左表名字\n",
        "            \"left_on\": [\"geography\"],  # 左表拼表键\n",
        "            \"right_entity\": \"geo_topo.csv\",  # 右表名字\n",
        "            \"right_on\": [\"geography_level_3\"], # 右表拼表键\n",
        "            \"type\": \"1-1\" # 左表与右表的连接关系\n",
        "        } # test.csv和geo_topo.csv两张表是1对1的关系，拼接键为test.csv中的geography列 和 geo_topo.csv中的geography_level_3列\n",
        "]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "3af307da",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3af307da",
        "outputId": "653bb71d-7ea0-494b-b8b0-250338f4fa4f",
        "scrolled": true
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "   INFO ->  [+] read demand_test_A.csv\n",
            "   INFO ->  Memory usage of dataframe is 3.78 MB\n",
            "   INFO ->  Memory usage after optimization is: 0.91 MB\n",
            "   INFO ->  Decreased by 75.8%\n",
            "   INFO ->  table = demand_test_A.csv, shape = (61936, 8)\n",
            "   INFO ->  [+] read demand_train_A.csv\n",
            "   INFO ->  Memory usage of dataframe is 17.38 MB\n",
            "   INFO ->  Memory usage after optimization is: 4.41 MB\n",
            "   INFO ->  Decreased by 74.6%\n",
            "   INFO ->  table = demand_train_A.csv, shape = (284832, 8)\n",
            "   INFO ->  [+] read geo_topo.csv\n",
            "   INFO ->  Memory usage of dataframe is 0.00 MB\n",
            "   INFO ->  Memory usage after optimization is: 0.00 MB\n",
            "   INFO ->  Decreased by -110.9%\n",
            "   INFO ->  table = geo_topo.csv, shape = (91, 3)\n",
            "   INFO ->  [+] read inventory_info_A.csv\n",
            "   INFO ->  Memory usage of dataframe is 0.04 MB\n",
            "   INFO ->  Memory usage after optimization is: 0.03 MB\n",
            "   INFO ->  Decreased by 19.9%\n",
            "   INFO ->  table = inventory_info_A.csv, shape = (632, 8)\n",
            "   INFO ->  [+] read product_topo.csv\n",
            "   INFO ->  Memory usage of dataframe is 0.00 MB\n",
            "   INFO ->  Memory usage after optimization is: 0.00 MB\n",
            "   INFO ->  Decreased by -149.5%\n",
            "   INFO ->  table = product_topo.csv, shape = (19, 2)\n",
            "   INFO ->  [+] read test.csv\n",
            "   INFO ->  Memory usage of dataframe is 2.84 MB\n",
            "   INFO ->  Memory usage after optimization is: 0.44 MB\n",
            "   INFO ->  Decreased by 84.5%\n",
            "   INFO ->  table = test.csv, shape = (61936, 6)\n",
            "   INFO ->  [+] read train.csv\n",
            "   INFO ->  Memory usage of dataframe is 15.21 MB\n",
            "   INFO ->  Memory usage after optimization is: 3.32 MB\n",
            "   INFO ->  Decreased by 78.2%\n",
            "   INFO ->  table = train.csv, shape = (284832, 7)\n",
            "   INFO ->  [+] read weight_A.csv\n",
            "   INFO ->  Memory usage of dataframe is 0.01 MB\n",
            "   INFO ->  Memory usage after optimization is: 0.02 MB\n",
            "   INFO ->  Decreased by -66.9%\n",
            "   INFO ->  table = weight_A.csv, shape = (632, 3)\n"
          ]
        }
      ],
      "source": [
        "autox = AutoX(target = 'qty', train_name = 'train.csv', test_name = 'test.csv', \n",
        "               id = ['unit'], path = path, time_series=True, ts_unit='D',time_col = 'ts',\n",
        "               relations = relations\n",
        "              )  #feature_type = feature_type,"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "df9582f9",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "df9582f9",
        "outputId": "4521d7b9-5995-4656-bbfb-bcb23792899a",
        "scrolled": true
      },
      "outputs": [],
      "source": [
        "sub = autox.get_submit_ts()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "22dce147",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 206
        },
        "id": "22dce147",
        "outputId": "2e57082a-688e-444b-a583-a19f08a1660c"
      },
      "outputs": [],
      "source": [
        "# 查看预测结果\n",
        "sub.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "eda5a3e2",
      "metadata": {
        "id": "eda5a3e2"
      },
      "outputs": [],
      "source": [
        "# 检查预测结果和真实结果的差距\n",
        "sub.rename({'qty': 'qty_pre'}, axis=1, inplace=True)\n",
        "demand_test_A = pd.read_csv(f'{path}/demand_test_A.csv', usecols = ['unit','ts','qty'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a778c10e",
      "metadata": {
        "id": "a778c10e"
      },
      "outputs": [],
      "source": [
        "analyze = demand_test_A.merge(sub, on = ['unit', 'ts'], how = 'left')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ee82689b",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 424
        },
        "id": "ee82689b",
        "outputId": "caa9c988-5015-4a6d-9dc8-192c473248f7",
        "scrolled": true
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>unit</th>\n",
              "      <th>ts</th>\n",
              "      <th>qty</th>\n",
              "      <th>qty_pre</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>0efede250ca3d05f9d4cc3609242d804</td>\n",
              "      <td>2021-03-02</td>\n",
              "      <td>3437.199978</td>\n",
              "      <td>3216.700945</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>fbb83aefc6f5d6f6bc22ae3ee757d327</td>\n",
              "      <td>2021-03-02</td>\n",
              "      <td>34.067925</td>\n",
              "      <td>51.150187</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>392aaa20e70b4d7539cc7a2e09562521</td>\n",
              "      <td>2021-03-02</td>\n",
              "      <td>34.856490</td>\n",
              "      <td>49.285081</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>2effa036807329a88056093fabb07ce6</td>\n",
              "      <td>2021-03-02</td>\n",
              "      <td>36677.666667</td>\n",
              "      <td>36168.939016</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>7dc25ea61b4d47f7de6c7a8d8d559487</td>\n",
              "      <td>2021-03-02</td>\n",
              "      <td>56688.333333</td>\n",
              "      <td>57862.451527</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>61931</th>\n",
              "      <td>27678e453a20126fee7e719fb4b6f933</td>\n",
              "      <td>2021-06-07</td>\n",
              "      <td>299.333333</td>\n",
              "      <td>270.521660</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>61932</th>\n",
              "      <td>a31b6fc53d57419631077bbfe4b241ba</td>\n",
              "      <td>2021-06-07</td>\n",
              "      <td>13695.666667</td>\n",
              "      <td>9152.683892</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>61933</th>\n",
              "      <td>8a54612bdaf867b47ca31e7ecc225021</td>\n",
              "      <td>2021-06-07</td>\n",
              "      <td>24504.666667</td>\n",
              "      <td>23744.845177</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>61934</th>\n",
              "      <td>9b8f48bacb1a63612f3a210ccc6286cc</td>\n",
              "      <td>2021-06-07</td>\n",
              "      <td>15317.428600</td>\n",
              "      <td>15288.069939</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>61935</th>\n",
              "      <td>b035f859cf03840b75abd80dc1cf3e94</td>\n",
              "      <td>2021-06-07</td>\n",
              "      <td>14.813658</td>\n",
              "      <td>36.699367</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>61936 rows × 4 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "                                   unit          ts           qty       qty_pre\n",
              "0      0efede250ca3d05f9d4cc3609242d804  2021-03-02   3437.199978   3216.700945\n",
              "1      fbb83aefc6f5d6f6bc22ae3ee757d327  2021-03-02     34.067925     51.150187\n",
              "2      392aaa20e70b4d7539cc7a2e09562521  2021-03-02     34.856490     49.285081\n",
              "3      2effa036807329a88056093fabb07ce6  2021-03-02  36677.666667  36168.939016\n",
              "4      7dc25ea61b4d47f7de6c7a8d8d559487  2021-03-02  56688.333333  57862.451527\n",
              "...                                 ...         ...           ...           ...\n",
              "61931  27678e453a20126fee7e719fb4b6f933  2021-06-07    299.333333    270.521660\n",
              "61932  a31b6fc53d57419631077bbfe4b241ba  2021-06-07  13695.666667   9152.683892\n",
              "61933  8a54612bdaf867b47ca31e7ecc225021  2021-06-07  24504.666667  23744.845177\n",
              "61934  9b8f48bacb1a63612f3a210ccc6286cc  2021-06-07  15317.428600  15288.069939\n",
              "61935  b035f859cf03840b75abd80dc1cf3e94  2021-06-07     14.813658     36.699367\n",
              "\n",
              "[61936 rows x 4 columns]"
            ]
          },
          "execution_count": 14,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "analyze"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "521d01ab",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "521d01ab",
        "outputId": "4d4273b2-4a94-4708-be84-337e34e49e9a"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "476.4757067233442"
            ]
          },
          "execution_count": 15,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# 查看mae\n",
        "from sklearn.metrics import mean_absolute_error\n",
        "y_true = analyze['qty']\n",
        "y_pred = analyze['qty_pre']\n",
        "mean_absolute_error(y_true, y_pred)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "1bFNurdnoFO7",
      "metadata": {
        "id": "1bFNurdnoFO7"
      },
      "source": [
        "更多使用案例请参考: [autox](https://github.com/4paradigm/autox?spm=5176.21852664.0.0.5594640eeR1PoH)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "081a435e",
      "metadata": {
        "colab": {
          "background_save": true
        },
        "id": "081a435e"
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "name": "“autox_tutorial_天池供应链.ipynb”的副本",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.8"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": false,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {
        "height": "calc(100% - 180px)",
        "left": "10px",
        "top": "150px",
        "width": "253px"
      },
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
